/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package camada.bancodedados.daoimplemente.cat52;

import camada.bancodedados.conexao.Conexao;
import camada.negocio.dao.cat52.ImportacaoCatDAO;
import eloma.classe.cat52.ImportacaoCat;
import eloma.classe.cat52.ImportacaoCatDetalhe;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Ícaro
 */
public class ImportacaoCatDAOImpl implements ImportacaoCatDAO {
    
    private static Connection con;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    
    @Override
    public int inserir(ImportacaoCat d) throws Exception {
        
        int key = 0;
        
        try {
            
            con = Conexao.getCon();
            con.setAutoCommit(false);
            
            final String SQL_SCRIPT = "INSERT INTO importacaocat(dataImportacao,diretorio)VALUES(?,?)";
            
            stmt = con.prepareStatement(SQL_SCRIPT);
            stmt.setDate(1, d.getDataImportacao());
            stmt.setString(2, d.getDiretorio());
            
            stmt.executeUpdate();
            
            String query = "select idRegistroE01 from registroe01";
            ResultSet rs = stmt.executeQuery(query);
            
            while (rs.next()) {
                key = rs.getInt("idImportacaoCat");
            }
            
            con.commit();
            
        } catch (SQLException e) {
            con.rollback();
        } catch (Exception e) {
            con.rollback();
        }
        
        return key;
        
    }
    
    @Override
    public void remover(ImportacaoCat d) throws Exception {
        
        try {
            
            final String SQL_SCRIPT = "delete from importacaocat where idImportacaoCat = ?";
            
            con = Conexao.getCon();
            con.setAutoCommit(false);
            
            stmt = con.prepareStatement(SQL_SCRIPT);
            stmt.setInt(1, d.getIdImportacaoCat52());
            stmt.executeUpdate();
            
            con.commit();
            
        } catch (SQLException e) {
            con.rollback();
        } catch (Exception e) {
            con.rollback();
        }
        
    }
    
    @Override
    public List<ImportacaoCat> buscaTodos() throws Exception {
        
        try {
            
            String SQL_SCRIPT = "Select * from importacaocat order by idImportacaoCat";
            
            con = Conexao.getCon();
            con.setAutoCommit(false);
            
            stmt = con.prepareStatement(SQL_SCRIPT);
            rs = stmt.executeQuery();
            
            con.commit();
            
        } catch (SQLException sqle) {
            con.rollback();
        } catch (Exception e) {
            con.rollback();
        }
        
        return carregarMultiplosResultados(rs);
        
    }
    
    public List<ImportacaoCat> carregarMultiplosResultados(ResultSet rs) throws SQLException {
        List<ImportacaoCat> resultList = new ArrayList<ImportacaoCat>();
        while (rs.next()) {
            ImportacaoCat dto = new ImportacaoCat();
            carregarVO(dto, rs);
            resultList.add(dto);
        }
        return resultList;
    }
    
    private ImportacaoCat carregarResultadoSimples(ResultSet rs) throws SQLException {
        if (rs.next()) {
            ImportacaoCat dto = new ImportacaoCat();
            carregarVO(dto, rs);
            return dto;
        } else {
            return null;
        }
    }
    
    private void carregarVO(ImportacaoCat dto, ResultSet rs) throws SQLException {
        try {
            
            dto.setIdImportacaoCat52(rs.getInt("IdImportacaoCat"));
            dto.setDataImportacao(rs.getDate("dataImportacao"));
            dto.setDiretorio(rs.getString("diretorio"));
            
            ImportacaoCatDetalheDAOImpl daoCatDetalhe = new ImportacaoCatDetalheDAOImpl();
            List<ImportacaoCatDetalhe> listaDetalhe = daoCatDetalhe.buscaTodosId(rs.getInt("IdImportacaoCat"));
            
            dto.setListaDetalhe(listaDetalhe);
            
        } catch (Exception e) {
            
        }
    }
}
